package io.renren.modules.carinfo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.renren.common.utils.PageData;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.carinfo.dto.CarInfoDTO;
import io.renren.modules.carinfo.entity.CarInfoEntity;
import io.renren.modules.carinfo.service.CarInfoService;
import io.renren.modules.carinfo.vo.CarInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;




/**
 * 车辆信息表
 */
@Api(tags = "车辆信息表")
@RestController
@RequestMapping("/carinfo")
public class CarInfoController {
    @Autowired
    private CarInfoService carInfoService;

    /**
     * 车辆信息表(分页)
     */
    @ApiOperation("车辆信息表(分页)")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageData<CarInfoVO> pageData = carInfoService.queryCarInfoPage(params);
        return R.ok().put("page", pageData);
    }


    /**
     * 根据id查看车辆信息
     */
    @ApiOperation("根据id查看车辆信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        CarInfoVO carInfoVO = carInfoService.queryById(id);
        return R.ok().put("carInfo", carInfoVO);
    }

    /**
     * 新增车辆信息
     */
    @ApiOperation("新增车辆信息")
    @PostMapping
    public R save(@RequestBody CarInfoDTO dto){
        // 验证参数
        ValidatorUtils.validateEntity(dto, AddGroup.class);
		carInfoService.saveCarInfo(dto);
        return R.ok();
    }

    /**
     * 修改车辆信息
     */
    @ApiOperation("修改车辆信息")
    @PutMapping
    public R update(@RequestBody CarInfoDTO dto){
        // 验证参数
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
		carInfoService.editById(dto);
        return R.ok();
    }

    /**
     * 删除车辆信息
     */
    @ApiOperation("删除车辆信息")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		carInfoService.deleteByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 车辆上下架(0:下架 1:上架)
     */
    @ApiOperation("车辆上下架(0:下架 1:上架)")
    @PutMapping("{id}/{status}")
    public R carShelve(@PathVariable Long id,@PathVariable Integer status){
        carInfoService.carShelve(id,status);
        return R.ok();
    }

    /**
     * 获取车辆编号
     */
    @ApiOperation("获取车辆编号")
    @GetMapping("/getCarNum")
    public R getCarNum(@RequestParam Map<String, Object> params){
        List<CarInfoEntity> carNums = carInfoService.getCarNum(params);
        return R.ok().put("carNums",carNums);
    }

}
